<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手牌号" prop="handBrand">
        <el-input
          v-model="queryParams.handBrand"
          placeholder="请输入手牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手牌类别" prop="handCategory">
        <el-select v-model="queryParams.handCategory" placeholder="请选择手牌类别" clearable>
          <el-option
            v-for="dict in dict.type.yqg_hand_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手牌状态" prop="handState">
        <el-select v-model="queryParams.handState" placeholder="请选择手牌状态" clearable>
          <el-option
            v-for="dict in dict.type.yqg_hand_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="锁定状态" prop="lockedState">
        <el-select v-model="queryParams.lockedState" placeholder="请选择锁定状态" clearable>
          <el-option
            v-for="dict in dict.type.yqg_locked_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <el-form v-show="showSearch">
      <el-form-item style="text-align: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:hands:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:hands:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="handsList" border @selection-change="handleSelectionChange"
              style="overfloW-x: auto;">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="index" label="序号" align="center" :index="getIndex"/>
      <el-table-column label="手牌号" align="center" prop="handBrand"/>
      <el-table-column label="手牌类别" align="center" prop="handCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yqg_hand_category" :value="scope.row.handCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="手牌状态" align="center" prop="handState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yqg_hand_state" :value="scope.row.handState"/>
        </template>
      </el-table-column>
      <el-table-column label="锁定状态" align="center" prop="lockedState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yqg_locked_state" :value="scope.row.lockedState"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改手牌基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="755px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
        <el-form-item label="手牌号" prop="handBrand" :rules="[{required: true, message: '手牌号不能为空'}]">
          <el-input v-model="form.handBrand" placeholder="请输入手牌号" style="width: 240px"/>
        </el-form-item>
        <el-form-item label="手牌类别" prop="handCategory" :rules="[{required: true, message: '未选择手牌类别'}]">
          <el-select v-model="form.handCategory" placeholder="请选择手牌类别" style="width: 240px">
            <el-option
              v-for="dict in dict.type.yqg_hand_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手牌状态" prop="handState" :rules="[{required: true, message: '未设置手牌状态'}]">
          <el-select v-model="form.handState" placeholder="请选择手牌状态" style="width: 240px">
            <el-option
              v-for="dict in dict.type.yqg_hand_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="锁定状态" prop="lockedState" :rules="[{required: true, message: '未设置锁定类别'}]">
          <el-select v-model="form.lockedState" placeholder="请选择锁定状态" style="width: 240px">
            <el-option
              v-for="dict in dict.type.yqg_locked_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listHands, getHands, delHands, addHands, updateHands} from "@/api/module/ljw/hands";

export default {
  name: "Hands",
  dicts: ['yqg_hand_category', 'yqg_hand_state', 'yqg_locked_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中id数组
      ids: [],
      // 选中手牌号数组
      hands: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 手牌基础信息表格数据
      handsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 手牌号
      hand: '',
      // 标志
      signal: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        handBrand: null,
        handCategory: null,
        handState: null,
        lockedState: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询手牌基础信息列表 */
    getList() {
      this.loading = true;
      listHands(this.queryParams).then(response => {
        this.handsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        handBrand: null,
        handCategory: null,
        handState: null,
        lockedState: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null
      };
      this.hand = ''
      this.resetForm("form");
    },
    getIndex(index) {
      return index + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.hands = selection.map(item => item.handBrand)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加手牌基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHands(id).then(response => {
        this.form = response.data;
        this.hand = this.form.handBrand
        this.open = true;
        this.title = "修改手牌基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        var re = /^[\d]*$/
        if (!re.test(this.form.handBrand)) {
          this.$modal.msgError("手牌号只能为数字")
          return
        }
        if (this.form.handBrand.includes(' ')) {
          this.$modal.msgError("手牌号中不能含有空格")
          return
        }
        if (valid) {
          if (this.form.id != null) {
            if (this.form.handBrand !== this.hand) {
              this.signal = true
            } else {
              this.signal = false
            }
            this.form.note1 = this.signal
            updateHands(this.form, this.signal).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.signal = false
              this.open = false;
              this.getList();
            });
          } else {
            addHands(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.signal = false
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const hand = row.handBrand || this.hands
      // this.$modal.confirm('是否确认删除手牌基础信息编号为"' + hand + '"的数据项？').then(function () {
      this.$modal.confirm('是否确认删除手牌号为"' + hand + '"的手牌？').then(function () {
        return delHands(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/hands/export', {
        ...this.queryParams
      }, `hands_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<!--
<style lang="scss" scoped>

.shounun{
  ::v-deep .el-input__inner{
    padding-right: 30px;
  }
}

</style>
-->
